<?php

namespace App\Component;

use \App\Model\Dao\BannerPosition,
    \App\Model\Dao\Banner as Ban,
    \Nette\Application\UI\Control;

class Banner extends Control {
  
  private $position;
  
  private $em;
  
  /**
   * Init component
   * @param int $positionCode
   * @param EntityManager $entityManager
   */
  public function init($positionCode, $entityManager){
    $this->position = $positionCode;
    $this->em = $entityManager;
  }

  /**
   * Render component
   */
	public function render() {
    
    $bannerPositionDao = $this->em->getDao(BannerPosition::getClassName());
    $bp = $bannerPositionDao->findOneBy(array('name' => $this->position));
    
    if(!$bp){
      $banner = false;
    }else{
      $bannerDao = $this->em->getDao(Ban::getClassName());
      $banner = $bannerDao->findBy(array('bannerposition' => $bp));
      shuffle($banner);
      
      if(isset($banner[0])){
        $banner = $banner[0];
      }else{
        $banner = false;
      }
    }
    
		$template = $this->template;
		$template->setFile(__DIR__ . '/banner.latte');
    $this->template->banner = $banner;
    
		$template->render();
	}

}
